﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _pageajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        public string sId = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Admin_Load("0000", "json");
            sId = q("sid");
            if (!Jumbot.Common.Validator.IsNumeric(sId))
            {
                this._response = JsonResult(0, "单页分组有误");
            }
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                case "ajaxCreatePage":
                    ajaxCreatePage();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            if (q("id") == "0")
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title";
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_page"))
                    this._response = JsonResult(0, "不可添加");
                else
                    this._response = JsonResult(1, "可以添加");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title and id<>" + q("id");
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_page"))
                    this._response = JsonResult(0, "不可修改");
                else
                    this._response = JsonResult(1, "可以修改");
            }
        }
        private void ajaxGetList()
        {
            doh.Reset();
            doh.SqlCmd = "Select [ID],[Title],[source] FROM [jcms_normal_page] WHERE [sId]= " + sId + " ORDER BY Id desc";
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\",returnval :\"操作成功\"," + Jumbot.Common.JsonHelp.DtToSON(dt) + "}";
        }
        private void ajaxDel()
        {
            string pId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=" + pId;
            doh.Delete("jcms_normal_page");
            this._response = JsonResult(1, "成功删除");
        }
        private void ajaxCreatePage()
        {
            string pId = f("id");
            JumbotCms.DataObject.Page sPage = new JumbotCms.DataObject.Page();
            sPage.GetAllColumn(pId, doh);

            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", sPage.SId);
            string tpPath = doh.GetValue("jcms_normal_page_sort", "Dir").ToString();
            if (tpPath.Length == 0)
            {
                this._response = JsonResult(0, "分组有误");
                return;
            }
            string PageStr = Jumbot.Common.DirFile.ReadFile("~/_data/page/" + tpPath + "_" + sPage.Source);
            JumbotCms.API.TemplateEngine te = new JumbotCms.API.TemplateEngine(doh, "0");
            te.IsHtml = site.IsHtml;
            te.PageNav = "<a href=\"/\">首页</a>&nbsp;&gt;&nbsp;" + sPage.Title;
            te.PageTitle = sPage.Title + "_" + site.Name + site.TitleTail;
            te.PageKeywords = site.Keywords;
            te.PageDescription = site.Description;
            te.ReplacePublicTag(ref PageStr, "0", te.DefaultSkinId);
            PageStr = PageStr.Replace("<@textarea ", "<textarea ");
            PageStr = PageStr.Replace("</textarea@>", "</textarea>");
            Jumbot.Common.DirFile.SaveFile(PageStr, site.Dir + tpPath + "/" + sPage.Source);
            this._response = JsonResult(1, "生成成功");
        }
    }
}